package cn.yogehaoren.message_properties;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class MessagePropertiesConsumer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1. 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过Connection创建Channel
        Channel channel = connection.createChannel();

        // 4. 声明一个队列
        String queueName = "test001";
        channel.queueDeclare(queueName, true, false, false, null);

        // 5. 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

        // 6. 设置channel
        channel.basicConsume(queueName, true, queueingConsumer);

        // 7. 获取消息
        while (true){

            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            System.out.println("#########################");
            String msg = new String(delivery.getBody());
            System.out.println("消费端: "+msg);
            AMQP.BasicProperties properties = delivery.getProperties();
            System.out.println("my1: "+ properties.getHeaders().getOrDefault("my1", "None"));
            System.out.println("my2: "+ properties.getHeaders().getOrDefault("my2", "None"));

        }



    }

}
